<?php
    /*
     module:		基本配置控制器
     create_time:	2022-08-10 18:44:12
     author:
     contact:
    */
    
    namespace app\admin\controller;
    
    use app\admin\model\Baseconfig as BaseconfigModel;
    use app\common\service\TenIM;
    use think\Model;
    
    class Baseconfig extends Model
    {
        
         protected $name = 'base_config';
        
        /*
         * @Description  配置表单
         */
        public function index()
        {
            if (!$this->request->isPost()) {
                return view('index');
            } else {
                $data = $this->request->post();
                $this->validate($data, \app\admin\validate\Baseconfig::class);
                
                $data['keyword'] = implode(',', $data['keyword']);
                $data['writer_label'] = implode(',', $data['writer_label']);
                $data['word_count_label'] = implode(',', $data['word_count_label']);
                
                if($data["kg_pipei"] == 0){
                    //需要获取所遇没权限 但是有订单的用户
                    $order =  db("ordertask")
                                ->alias("o")
                                ->join("membe u","o.uid=u.membe_id","left")
                                ->where("u.is_pai=0")
                                ->where("o.status=0")
                                ->update(["o.status"=>2,"o.qx_time"=>time()]);
                }
                
                
                $info = BaseconfigModel::column('data', 'name');
                foreach ($data as $key => $value) {
                    if (array_key_exists($key, $info)) {
                        BaseconfigModel::field('data')->where(['name' => $key])->update(['data' => $value]);
                    } else {
                        BaseconfigModel::create(['name' => $key, 'data' => $value]);
                    }
                }
                $im = new TenIM();
                $sysIm['uid'] = 'administrator';
                $sysIm['username'] = $data['sys_msg_nickname']??'系统消息通知';
                $sysIm['pic'] = $data['sys_msg_avatar']??$data['user_avatar'];
                
               
                $im->editUserInfo($sysIm);
                return json(['status' => 200, 'msg' => '操作成功']);
            }
        }
        
        public function finance()
        {
            if (!$this->request->isPost()) {
                return view('finance');
            } else {
                $data = $this->request->post();
                $this->validate($data, \app\admin\validate\Baseconfig::class);
                
                $data['keyword'] = implode(',', $data['keyword']);
                $data['writer_label'] = implode(',', $data['writer_label']);
                $data['word_count_label'] = implode(',', $data['word_count_label']);
                
                $info = BaseconfigModel::column('data', 'name');
                foreach ($data as $key => $value) {
                    if (array_key_exists($key, $info)) {
                        BaseconfigModel::field('data')->where(['name' => $key])->update(['data' => $value]);
                    } else {
                        BaseconfigModel::create(['name' => $key, 'data' => $value]);
                    }
                }
                $im = new TenIM();
                $sysIm['uid'] = 'administrator';
                $sysIm['username'] = $data['sys_msg_nickname']??'系统消息通知';
                $sysIm['pic'] = $data['sys_msg_avatar']??$data['user_avatar'];
                
                $im->editUserInfo($sysIm);
                return json(['status' => 200, 'msg' => '操作成功']);
            }
        }
        
        
        /*
         * @Description  修改信息之前查询信息的 勿要删除
         */
        function getInfo()
        {
            $res = BaseconfigModel::column('data', 'name');
            $res['keyword'] = explode(',', $res['keyword']);
            $res['writer_label'] = explode(',', $res['writer_label']);
            $res['word_count_label'] = explode(',', $res['word_count_label']);
            
            $res['water_alpha'] = (int)$res['water_alpha'];
            
            $data['status'] = 200;
            $data['data'] = $res;
            return json($data);
        }
        
        
    }

